<!--添加预约页面-->
<template>
<el-row class="container">
    <el-col :span="24" style="min-height:60px;height:5%;background-color: #014171;">
        <el-row>
            <el-col :span="10">
                <div class="logo">
                    <el-button type="primary" icon="el-icon-back" @click="showMainPage">&nbsp;&nbsp;{{$t("appointment.returnBtnName")}}&nbsp;&nbsp;</el-button>
                </div>
            </el-col>
            <el-col :span="14">
            </el-col>
        </el-row>
    </el-col>

    <el-col :span="24" style="margin-top:20px;padding-left:20px;padding-right:20px;">
        <choiceStaff ref="choiceStaff" :clickEvent="true" @click="selectStaff" :checkAuth="true"></choiceStaff>
    </el-col>

    <el-col :span="24" style="height:5%;margin-top:10px;padding-left:20px;padding-right:20px;width:100%;">
        <el-form ref="addForm" :model="addForm" :rules="addFormRules" label-width="100px" style="background-color:#ffffff;padding:30px 30px 5px 30px;">
            <el-form-item :label="$t('appointment.addEditForm.clientNameLabel')">
                <el-col :span="12">
                    <el-form-item prop="firstName">
                        <el-input v-model="addForm.firstName" :placeholder="$t('appointment.addEditForm.clientNameLabelFirstName')" style="width: 100%;"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item prop="familyName">
                        <el-input v-model="addForm.familyName" :placeholder="$t('appointment.addEditForm.clientNameLabelFamilyName')" style="width: 100%;"></el-input>
                    </el-form-item>
                </el-col>
            </el-form-item>
            <el-form-item :label="$t('appointment.addEditForm.appointmentDateTimeLabel')">
                <el-col :span="12">
                    <el-form-item prop="appointmentDate">
                        <el-date-picker v-model="addForm.appointmentDate" type="date" :placeholder="$t('appointment.addEditForm.appointmentDateLabel')" style="width:100%" :picker-options="pickerOptions1">
                        </el-date-picker>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item prop="appointmentTime">
                        <el-time-picker arrow-control v-model="addForm.appointmentTime" :picker-options="{selectableRange: '00:00:00 - 23:59:59'}" :placeholder="$t('appointment.addEditForm.appointmentTimeLabel')" style="width:100%">
                        </el-time-picker>
                    </el-form-item>
                </el-col>
            </el-form-item>
            <el-form-item :label="$t('appointment.addEditForm.clientPhoneLabel')" prop="clientPhone">
                <el-input v-model="addForm.clientPhone">
                    <i slot="prefix" class="el-input__icon el-icon-phone-outline"></i>
                </el-input>
            </el-form-item>
            <el-form-item :label="$t('appointment.addEditForm.remarkLabel')">
                <el-input type="textarea" v-model="addForm.remark"></el-input>
            </el-form-item>
            <el-form-item style="text-align:right">
                <el-button type="primary" icon="el-icon-check" @click="saveAppointment">{{$t('appointment.addEditForm.saveBtn')}}</el-button>
            </el-form-item>
        </el-form>
    </el-col>
</el-row>
</template>

<script>
import util from '../../common/js/util';
import ChoiceStaff from '../../components/choiceStaff.vue';
import jwt_decode from 'jwt-decode';
import {
    addAppointment
} from '../../api/api';
export default {
    components: {
        choiceStaff: ChoiceStaff
    },
    data() {
        return {
            addForm: {
                staffId: '',
                firstName: '',
                familyName: '',
                appointmentDate: new Date(),
                appointmentTime: '',
                clientPhone: '',
                remark: ''
            },
            addFormRules: {
                familyName: [{
                    //required: true,
                    message: this.$t('appointment.addEditForm.clientNameLabelFamilyNameRule'),
                    trigger: 'blur'
                }],
                firstName: [{
                    //required: true,
                    message: this.$t('appointment.addEditForm.clientNameLabelFirstNameRule'),
                    trigger: 'blur'
                }],
                appointmentDate: [{
                    required: true,
                    message: this.$t('appointment.addEditForm.appointmentDateLabelRule'),
                    trigger: 'blur'
                }],
                appointmentTime: [{
                    required: true,
                    message: this.$t('appointment.addEditForm.appointmentTimeLabelRule'),
                    trigger: 'blur'
                }],
                clientPhone: [{
                    //required: true,
                    message: this.$t('appointment.addEditForm.clientPhoneLabelRule'),
                    trigger: 'blur'
                }]
            },
            pickerOptions1: {
                disabledDate(time) {
                    return time.getTime() < Date.now() - 8.64e7;
                }
            }
        };
    },
    mounted() {
        if (sessionStorage.getItem('userToken') != null) {
            var userToken = jwt_decode(sessionStorage.getItem("userToken"));
            if (userToken.roleId == 1) {
                this.addForm.staffId = userToken.userid;
            }
            // if (userToken.roleId == 2) {
            //     this.addForm.staffId = userToken.userid;
            //     this.$refs.choiceStaff.invokeStaff(this.addForm.staffId);
            // }
        }
    },
    methods: {
        showMainPage() {
            //显示预约页面
            this.$router.push({
                path: '/appointment_list'
            });
        },
        saveAppointment() {
            //必须先选择预约的员工
            if (this.addForm.staffId === '') {
                this.$message({
                    message: this.$t('appointment.addEditForm.choiceStaffRule'),
                    type: 'error'
                });
                return;
            }
            this.$refs.addForm.validate((valid) => {
                if (valid) {
                    this.$confirm(this.$t('appointment.addEditForm.addFormSaveBtnConfirmDialog.content'), this.$t('appointment.addEditForm.addFormSaveBtnConfirmDialog.title'), {
                        type: 'warning'
                    }).then(() => {
                        let para = Object.assign({}, this.addForm);
                        para.appointmentDate = (!para.appointmentDate || para.appointmentDate == '') ? '' : util.formatDate.format(new Date(para.appointmentDate), 'yyyy-MM-dd');
                        para.appointmentTime = (!para.appointmentTime || para.appointmentTime == '') ? '' : util.formatDate.format(new Date(para.appointmentTime), 'hh:mm:ss');
                        para.time = para.appointmentDate + " " + para.appointmentTime;
                        addAppointment(para).then(res => {
                            let {
                                msg,
                                code
                            } = res;
                            if (code !== "0") {
                                this.$message({
                                    message: msg,
                                    type: 'error'
                                });
                            } else {
                                this.$router.push({
                                    path: '/appointment_list'
                                });
                            }
                        });

                    }).catch(() => {

                    });
                }
            });
        },
        selectStaff(user) {
            this.addForm.staffId = user.id;
        }
    }
}
</script>

<style lang="scss" scoped>
@import '~scss_vars';

.container {
    position: absolute;
    top: 0px;
    bottom: 0px;
    width: 100%;
    height: 100%;
    overflow: hidden;
    background-color: #012547;
    font-family: Helvetica, Tahoma, Arial, sans-serif;
    font-size: 16px;
    line-height: 1.5;
    color: #ffffff;
}

.nav {
    display: flex;
    justify-content: Center;
    align-items: Center;
    font-family: "Arial", "Microsoft YaHei", "黑体", "宋体", sans-serif;
    font-size: 48px;
}

.userinfo {
    display: flex;
    justify-content: left;
    align-items: Center;
    text-align: right;
    padding-right: 35px;
    float: right;
    height: 60px;
}

.logo {
    display: flex;
    justify-content: left;
    align-items: Center;
    height: 60px;
    font-size: 26px;
    padding-left: 20px;
    padding-right: 20px;
}
</style>
